import '@/assets/main.css'

import { createApp } from 'vue'
import App from '@/App.vue'
import router from "@/router/index.js";
// 导入 Element Plus 图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import 'animate.css';
import '@/permission.js';
import 'nprogress/nprogress.css'
// 引入全局状态管理 Pinia
import pinia from '@/stores'
import {showPageLoading} from "@/composables/util.js";
import {getToken} from "@/composables/cookie.js";
import {useBlogSettingsStore} from "@/stores/blogsettings.js";

// 图片点击放大
import 'viewerjs/dist/viewer.css'
import VueViewer from 'v-viewer'



const app =createApp(App);
// 引入图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}


// 全局路由前置守卫
router.beforeEach((to, from, next) => {
    console.log('==> 全局路由前置守卫')

    // 展示页面加载 Loading
    showPageLoading()

    let token = getToken()

    if (!token && to.path.startsWith('/admin')) {
        // 省略...
    } else if (token && to.path === '/login') {
        // 省略...
    } else if (!to.path.startsWith('/admin')) {
        // 如果访问的非 /admin 前缀路由
        // 引入博客设置 store
        let blogSettingsStore = useBlogSettingsStore()
        // 获取博客设置信息并保存到全局状态中
        blogSettingsStore.getBlogSettings()
        next()
    } else {
        next()
    }
})


app.use(router)
// 应用 Pinia
app.use(pinia)
// 图片点击放大
app.use(VueViewer)
app.mount('#app')
